package com.android.videotest.music.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaDescriptionCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.android.videotest.R;
import com.android.videotest.music.base.BaseMusicActivity;
import com.android.videotest.music.ui.musiclist.MusicListAdapter;
import com.android.videotest.music.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class FilterPlayListActivity extends BaseMusicActivity {

    private RecyclerView mRecyclerView;
    private MusicListAdapter mListAdapter;
    private List<MediaBrowserCompat.MediaItem> mChildren;


    public static void start(Context context) {
        Intent starter = new Intent(context, FilterPlayListActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_play_list);

        findViewById(R.id.backButton).setOnClickListener(v -> finish());
        EditText editText = findViewById(R.id.cleanableEditText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ArrayList<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();
                for (int i = 0; i < mChildren.size(); i++) {
                    MediaBrowserCompat.MediaItem item = mChildren.get(i);
                    MediaDescriptionCompat description = item.getDescription();
                    String title = (String) description.getTitle();
                    if (title != null && title.contains(s)) {
                        mediaItems.add(item);
                    }
                }
                mListAdapter.setMediaItems(mediaItems);
            }
        });
        mRecyclerView = findViewById(R.id.recyclerView);
        mListAdapter = new MusicListAdapter();
        mListAdapter.setOnItemClickListener((item, position) -> {
            Bundle extras = new Bundle();
            extras.putInt("position", position);
            MediaControllerCompat.getMediaController(mActivity).getTransportControls().playFromMediaId(item.getMediaId(), extras);
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mListAdapter);
    }

    @Override
    protected void onConnected(@NonNull MediaControllerCompat mediaController) {
//        try {
//            // 保存控制器
        MediaControllerCompat.setMediaController(mActivity, mediaController);
//            // 完成构建UI
//            buildTransportControls();
//        } catch (RemoteException e) {
//            e.printStackTrace();
//        }
//        mMediaBrowserHelper.unsubscribe(mediaBrowser.getRoot(), mediaBrowserSubscriptionCallback);
//        mediaBrowser.subscribe(mediaBrowser.getRoot(), mediaBrowserSubscriptionCallback);
        Log.d(this.getClass().getSimpleName() + "-App", "onConnected() called with: mediaController = [" + mediaController + "]");
    }

    @Override
    protected void onChildrenLoaded(@NonNull String parentId, @NonNull List<MediaBrowserCompat.MediaItem> children) {
        mChildren = children;
//        mListAdapter.setMediaItems(children);
//        String lastMediaId = Utils.getLastMusic(mContext);
//        mListAdapter.setPlayingMediaId(lastMediaId);
        Log.d(this.getClass().getSimpleName() + "-App", "onChildrenLoaded() called with: parentId = [" + parentId + "], children = [" + children + "]");
    }

    @Override
    protected void onDisconnected() {
        Log.d(this.getClass().getSimpleName() + "-App", "onDisconnected() called");
    }

    @Override
    protected void onPlaybackStateChanged(PlaybackStateCompat state) {
        Log.d(this.getClass().getSimpleName() + "-App", "onPlaybackStateChanged() called with: state = [" + state + "]");
    }

    @Override
    protected void onMetadataChanged(MediaMetadataCompat metadata) {
        Log.d(this.getClass().getSimpleName() + "-App", "onMetadataChanged() called with: metadata = [" + metadata + "]");
    }


}
